package com.tuyan.aixiang.component;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.common.lang.VoidFunction;
import com.tuyan.aixiang.model.YsDevice;
import com.tuyan.aixiang.util.ConvertUtil;
import com.tuyan.aixiang.util.Log;
import com.tuyan.aixiang.util.VibratorUtil;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.EZOpenSDK;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PTZBtn extends RelativeLayout {

    private static final String TAG = "PTZBtn";

    @BindView(R.id.rl_up)
    RelativeLayout rlUp;

    @BindView(R.id.rl_down)
    RelativeLayout rlDown;

    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;

    @BindView(R.id.rl_right)
    RelativeLayout rlRight;

    @BindView(R.id.iv_close)
    ImageView ivClose;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.rl_wrapper)
    RelativeLayout rlWrapper;

    @BindView(R.id.iv_bg)
    ImageView ivBg;

    private YsDevice ysDevice;

    public PTZBtn(Context context) {
        this(context, null);
    }

    public PTZBtn(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PTZBtn(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = LayoutInflater.from(context).inflate(R.layout.layout_ptz_btn, this);
        ButterKnife.bind(contentView, this);
        ivClose.setOnClickListener(v -> {
            setVisibility(GONE);
            if (null != this.onCloseCb) {
                this.onCloseCb.call();
            }
        });

        buildListener(rlUp, EZConstants.EZPTZCommand.EZPTZCommandUp);
        buildListener(rlRight, EZConstants.EZPTZCommand.EZPTZCommandRight);
        buildListener(rlDown, EZConstants.EZPTZCommand.EZPTZCommandDown);
        buildListener(rlLeft, EZConstants.EZPTZCommand.EZPTZCommandLeft);
    }

    private VoidFunction onCloseCb;

    public void setOnClose(VoidFunction func) {
        this.onCloseCb = func;
    }

    public void setup(YsDevice device, boolean isFull) {
        this.ysDevice = device;
        operating = false;
        if (isFull) {
            setBackgroundColor(getResources().getColor(R.color.colorMask));
            tvTitle.setTextColor(getResources().getColor(R.color.colorPure));
            LayoutParams lp = (LayoutParams) rlWrapper.getLayoutParams();
            int size = ConvertUtil.dp2px(getContext(), 150);
            lp.width = size;
            lp.height = size;
            rlWrapper.setLayoutParams(lp);
            ivBg.setImageResource(R.drawable.circle_big_white);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private void buildListener(RelativeLayout btn, EZConstants.EZPTZCommand command) {
        btn.setOnTouchListener((v, event) -> {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                VibratorUtil.vibrateSlight(getContext());
                controlPtz(command, EZConstants.EZPTZAction.EZPTZActionSTART);
            }

            if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
                controlPtz(command, EZConstants.EZPTZAction.EZPTZActionSTOP);
            }

            return true;
        });
    }

    private boolean operating = false;

    private void controlPtz(EZConstants.EZPTZCommand command, EZConstants.EZPTZAction action) {
        new Thread(() -> {
            try {
                if (null == ysDevice) return;
                if (action != EZConstants.EZPTZAction.EZPTZActionSTOP && operating) return;
                operating = true;
                boolean success = EZOpenSDK.controlPTZ(
                        ysDevice.getDeviceSerial(),
                        ysDevice.getChannelNo(),
                        command,
                        action,
                        EZConstants.PTZ_SPEED_DEFAULT
                );
                operating = false;
                if (!success) {
                    Log.d(TAG, "命令发送失败");
                } else {
                    Log.d(TAG, "命令发送成功");
                }
            } catch (BaseException e) {
                Log.d(TAG, "命令发送失败");
                e.printStackTrace();
                operating = false;
            }
        }).start();

    }

//    private void setListener()
}
